package com.xiaoxx.utils;

/**
 * @author :HX
 * @date : 2023/8/1
 */

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * <p>AES加解密工具类</p>
 *
 * @author zhuquanwen
 * @version 1.0
 * @date 2018/7/13 17:31
 * @since jdk1.8
 **/
@SuppressWarnings("unused")
public final class AesUtil {
    private static final String SECRET_KEY = "XIAOXXABCDEFJZYX";

    /**
     * 加密
     * @param input
     * @return
     */
    public static String encrypt(String input) {
        try {
            SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encrypted = cipher.doFinal(input.getBytes());
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解密
     * @param input
     * @return
     */
    public static String decrypt(String input) {
        try {
            SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(input));
            return new String(decrypted);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
         System.out.println(encrypt("18381530837"));
            System.out.println(decrypt("bIbbPT3kL1TWOF9GYg5gwQ=="));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
